using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;

namespace IUPUI.StellarResults.Drawing
{
	/// <summary>
	/// Utilities to assist with Drawing.
	/// </summary>
	public static class DrawingUtilities
	{
		/// <summary>
		/// Gets the grid points.
		/// </summary>
		/// <param name="size">The size.</param>
		/// <param name="divisions">The divisions.</param>
		/// <returns></returns>
		public static Rectangle[] GetGridPoints(
			int size,
			int divisions)
		{
			List<Rectangle> points = new List<Rectangle>();

			// Ensure that divisions are in an acceptable range
			if (divisions > 100)
			{
				divisions = 100;
			}
			if (divisions < 1)
			{
				divisions = 1;
			}

			// Get interval range
			int interval = size / divisions;

			int x = 1;
			int y = 1;
			while (x < size)
			{
				y = 1;
				while (y < size)
				{
					points.Add(new Rectangle(x, y, interval, interval));
					y += interval;
				}

				x += interval;
			}

			return points.ToArray();
		}

		/// <summary>
		/// Converts the coordinates to point.
		/// </summary>
		/// <param name="coordinates">The coordinates.</param>
		/// <param name="radius">The radius.</param>
		/// <returns></returns>
		public static Point ConvertCoordinatesToPoint(
			ININ.GIS.Coordinates coordinates,
			decimal radius)
		{
			// determine x, y in terms of this central point
			// radius is in Pixels.
			// sin(ViewRadius)= radius
			// sin(Longitude) is porportional: sin(ViewRadius)/ radius = sin(Longitude) / x
			// solve for x, the number of pixels to represent the distance
			return new Point(
				(int)((double)radius * Math.Sin(coordinates.Longitude * Math.PI / 180) / Math.Sin((double)radius * Math.PI / 180)),
				(int)((double)radius * Math.Sin(coordinates.Latitude * Math.PI / 180) / Math.Sin((double)radius * Math.PI / 180))
			);
		}
	}
}
